import Vuex from 'vuex'
import event from '@/net/event'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    userAdcd: '',
    sbcj: '',
    sbcjData: [],
    adcd: '',
    adcdName: [],
    adcdId: [],
    type: '1',
    model: '',
    modelData: [],
    keyword: '',
    listData: []
  },
  mutations: {
    changeUserAdcd (state, value) {
      state.userAdcd = value
    },
    changeSbcj (state, value) {
      state.sbcj = value
    },
    changeSbcjData (state, value) {
      state.sbcjData = value
    },
    changeAdcd (state, value) {
      state.adcd = value
    },
    changeAdcdName (state, value) {
      state.adcdName = value
    },
    changeAdcdId (state, value) {
      state.adcdId = value
    },
    changeType (state, value) {
      state.type = value
    },
    changeModel (state, value) {
      state.model = value
    },
    changeModelData (state, value) {
      state.modelData = value
    },
    changeKeyword (state, value) {
      state.keyword = value
    },
    changeListData (state, value) {
      state.listData = value
    }
  },
  actions: {
    getSBCJ ({state, commit}, vm) {
      vm.$notice.loading.show()
      event.SBCJ(state.adcd).then(res => {
        let arr = res.result.datalist
        arr.unshift({
          GSDM: '',
          GSMC: '全部'
        })
        commit('changeSbcjData', arr)
        vm.$nextTick(() => {
          vm.$notice.loading.hide()
        })
      })
    },
    getSBXH ({state, commit}, vm) {
      vm.$notice.loading.show()
      event.SBXH(state.sbcj).then(res => {
        let arr = res.result.datalist
        arr.forEach(value => {
          value.NAME = `${value.SBLXMC}[${value.SBXH}]`
        })
        arr.unshift({
          SBLX_ID: '',
          NAME: '全部'
        })
        commit('changeModelData', arr)
        vm.$nextTick(() => {
          vm.$notice.loading.hide()
        })
      })
    },
    getAzddList ({state, commit}, vm) {
      vm.$notice.loading.show()
      event.AzddList({
        xzqhdm: state.adcd,
        datafrom: state.type,
        keyword: state.keyword,
        sbcj: state.sbcj,
        sbxh: state.model
      }).then(res => {
        commit('changeListData', res.result.datalist)
        vm.$nextTick(() => {
          vm.$notice.loading.hide()
        })
      })
    }
  }
})
